package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.ScheduleManagerActivity;
import com.lzx.iteam.bean.ScheduleManager;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.RoundImageView;

import java.util.ArrayList;

/**
 * Created by Megan on 16/3/14.
 */
public class ScheduleManagerAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ScheduleManager> list;
    private int mFlag;  // 1:日程管理员  2:审批管理 3：选择审批人

    public ScheduleManagerAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void bindData(ArrayList<ScheduleManager> list,int flag) {
        this.list = list;
        this.mFlag = flag;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        final ScheduleManager memberMsg = list.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.schedule_manager_item, null);
            holder.userImage = (RoundImageView) convertView.findViewById(R.id.iv_schedule_manager_image);
            holder.userName = (TextView) convertView.findViewById(R.id.tv_schedule_manager_name);
            holder.userPosition = (TextView) convertView.findViewById(R.id.tv_schedule_manager_position);
            holder.delete = (ImageView) convertView.findViewById(R.id.iv_schedule_manager_delete);
            holder.select = (ImageView) convertView.findViewById(R.id.iv_ap_leader);
            holder.mask = convertView.findViewById(R.id.select_mask);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.userName.setText(memberMsg.getUserName());
        ImageLoaderInterface.imageLoader.displayImage(memberMsg.getAvatar(), holder.userImage, ImageLoaderInterface.optionHeadImage);
        if (StringUtil.isEmpty(memberMsg.getJob())) {
            holder.userPosition.setVisibility(View.GONE);
        } else {
            holder.userPosition.setVisibility(View.VISIBLE);
            holder.userPosition.setText(memberMsg.getJob());
        }

        if (mFlag == 4){
            holder.delete.setImageResource(R.drawable.arrow_right_black);
        }else {
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    ((ScheduleManagerActivity) mContext).delScheduleManager(memberMsg.getUserId(),memberMsg.getContact_id());
                }
            });
        }
        if (mFlag == 3){
            holder.delete.setVisibility(View.GONE);
            if (ScheduleManagerActivity.mSelectPosition == position){
                holder.select.setVisibility(View.VISIBLE);
            }else{
                holder.select.setVisibility(View.GONE);
            }
            if (!StringUtil.isEmpty(memberMsg.getAttendance()) && "0".equals(memberMsg.getAttendance())){
                holder.mask.setVisibility(View.VISIBLE);
            }else{
                holder.mask.setVisibility(View.GONE);
            }
        }
        return convertView;
    }


    private class ViewHolder {
        RoundImageView userImage;
        TextView userName;
        TextView userPosition;
        ImageView delete;
        ImageView select;
        View mask;
    }

}
